package com.xudow.app.dynamicstate_old.module.discovery.search;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.beam.expansion.BeamBaseActivity;
import com.jude.utils.JUtils;
import com.xudow.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuchenxi on 16/6/24.
 */

public class SearchActivity extends BeamBaseActivity {

    @BindView(R.id.search)
    TextView search;
//    private DynamicSearchFragment mDynamicSearchFragment;
//    private UserSearchFragment mUserSearchFragment;

    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_search)
    EditText tvSearch;
//    @BindView(R.id.tablayout)
//    TabLayout tablayout;
//    @BindView(R.id.viewpager)
//    ViewPager viewpaer;

    //    PageAdapter mAdapter;
    SearchContentFragment searchContentFragment;
    SearchHistoryFragment searchHistoryFragment;
    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
//        mAdapter = new PageAdapter(getSupportFragmentManager());
//        viewpager.setAdapter(mAdapter);
//        tablayout.setupWithViewPager(viewpager);
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        search.setOnClickListener(v -> {
            String key = tvSearch.getText().toString();
            if (TextUtils.isEmpty(key)) {
                JUtils.Toast("请输入关键字");
                return;
            }
            String history = JUtils.getSharedPreference().getString("search_history", "");
            if (TextUtils.isEmpty(history))
                history = key;
            else {
                boolean hase = false;
                String[] histories = history.split(",");
                for (String temp : histories) {
                    if (temp.equals(key)) {
                        hase = true;
                        break;
                    }
                }
                if (!hase)
                    history = history + "," + key;
            }
//            history = (TextUtils.isEmpty(history) ? "" : history + ",") + key;
            JUtils.getSharedPreference().edit().putString("search_history", history).apply();
            search(key);
//            mDynamicSearchFragment.search(tvSearch.getText().toString());
//            new Handler().postDelayed(() -> mUserSearchFragment.search(tvSearch.getText().toString()),200);
        });
        transaction.add(R.id.fl_container, searchContentFragment = new SearchContentFragment(), "content");
        transaction.add(R.id.fl_container, searchHistoryFragment = new SearchHistoryFragment());
        transaction.hide(searchContentFragment).commit();
    }

    public void search(String searchText) {
        tvSearch.setText(searchText);
        fragmentManager.beginTransaction().hide(searchHistoryFragment).commit();
        fragmentManager.beginTransaction().show(searchContentFragment).commit();
        searchContentFragment.search(searchText);
    }

//    private class PageAdapter extends FragmentStatePagerAdapter {
//
//        public PageAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            switch (position) {
//                case 0:
//                    return mDynamicSearchFragment = new DynamicSearchFragment();
//                case 1:
//                    return mUserSearchFragment = new UserSearchFragment();
//                default:
//                    return null;
//            }
//        }
//
//        @Override
//        public CharSequence getPageTitle(int position) {
//            switch (position) {
//                case 0:
//                    return "内容";
//                case 1:
//                    return "用户";
//                default:
//                    return null;
//            }
//        }
//
//        @Override
//        public int getCount() {
//            return 2;
//        }
//    }
}
